package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.submitArticle.bean.DraftResponse;
import com.rongshu.view.userInfo.iview.PreviewView;
import com.rongshu.view.userInfo.presenter.PreviewPresenter;
import com.utils.DensityUtil;
import com.zzhoujay.richtext.RichText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PreviewActivity extends BaseActivity<PreviewPresenter> implements PreviewView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.txt_article_title)
    TextView txt_article_title;
    @BindView(R.id.txt_content)
    TextView txt_content;
    @BindView(R.id.layout_cover)
    RelativeLayout layout_cover;
    @BindView(R.id.iv_cover)
    ImageView iv_cover;
    @BindView(R.id.iv_head_icon)
    ImageView iv_head_icon;
    @BindView(R.id.iv_vip)
    ImageView iv_vip;
    @BindView(R.id.txt_article_author)
    TextView txt_article_author;
    //intent
    private Intent intent = null;
    private String title = null;
    private String topImg = null;
    private int contribute_id = 0;
    //map
    private Map<String, Object> map = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_preview_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new PreviewPresenter(this), this);
        //title
        txt_top_title.setText("预览");
        //intent
        intent = getIntent();
        if (intent == null) {
            return;
        }
        title = intent.getStringExtra(Constant.INTENT_TITLE);
        topImg = intent.getStringExtra(Constant.INTENT_DATA);
        contribute_id = intent.getIntExtra(Constant.INTENT_MESSAGE, contribute_id);
        RichText.initCacheDir(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getArticleBody();
    }

    private void refreshView(String body) {
        txt_article_title.setText(title);
        if (!TextUtils.isEmpty(topImg)) {
            layout_cover.setVisibility(View.VISIBLE);
            RequestOptions options_bg = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.bar_grey);
            SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    iv_cover.setImageDrawable(resource);
                    ViewGroup.LayoutParams lp = iv_cover.getLayoutParams();
                    lp.width = DensityUtil.getScreenWidth(PreviewActivity.this);
                    lp.height = lp.width / 4 * 3;
                    iv_cover.setLayoutParams(lp);
                }
            };
            Glide.with(this)
                    .load(topImg)
                    .apply(options_bg)
                    .into(simpleTarget);
        } else {
            layout_cover.setVisibility(View.GONE);
        }
        //头像
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.user_head_icon);
        Glide.with(this)
                .load(UserInfoUtils.getAvatar())
                .apply(options)
                .into(iv_head_icon);
        //vip
        iv_vip.setVisibility(UserInfoUtils.getVip() ? View.VISIBLE : View.GONE);
        //作者
        txt_article_author.setText(UserInfoUtils.getBiName());
        RichText.from(body).into(txt_content);
    }

    /**
     * 获取单个文章body
     */
    private void getArticleBody() {
        map = new HashMap<>();
        map.put("contribute_id", contribute_id);
        presenter.getArticleBody((int) map.get("contribute_id"),
                Common.encryptionSign(map));
        map.clear();
        showLoading(true);
    }

    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * 获取单个文章body
     *
     * @param response
     */
    @Override
    public void onArticleBody(DraftResponse response) {
        if (response.getErrCode() == 0) {
            showLoading(false);
            String body = response.getResponse().getArticleBody();
            refreshView(body);
        } else {
            toast(response.getErrMsg());
        }
    }
}
